package com.minhua.jijiehao2.mainframe.circle.moreTypeActivity.impl

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.CircleServiceDetailTypeBean
import com.minhua.jijiehao2.bean.CircleServiceTypeBean
import com.minhua.jijiehao2.mainframe.circle.businessListActivity.impl.BusinessListActivity
import com.minhua.jijiehao2.mainframe.circle.moreTypeActivity.imp.IMoreTypeView
import com.minhua.jijiehao2.view.NoScrollGridView
import kotlinx.android.synthetic.main.activity_more_type.*

class MoreTypeActivity : BaseActivity<IMoreTypeView, MoreTypePresenter>(), IMoreTypeView {

    override fun getLayoutId(): Int = R.layout.activity_more_type

    override fun getPresenter(): MoreTypePresenter = MoreTypePresenter()

    companion object {
        fun intentStart(context: Context, data: ArrayList<CircleServiceTypeBean>) {
            val intent = Intent(context, MoreTypeActivity::class.java)
            intent.putExtra("data", data)
            context.startActivity(intent)
        }
    }

    private val listData = arrayListOf<CircleServiceTypeBean>()
    private var adapter: BaseCommonAdapter<CircleServiceTypeBean>? = null

    override fun initView() {
        adapter = object : BaseCommonAdapter<CircleServiceTypeBean>(this, R.layout.item_more_type, listData) {
            override fun convert(holder: ViewHolder?, t: CircleServiceTypeBean?) {
                holder?.setText(R.id.tvType, t?.type_name)
                val gridView = holder?.getView<NoScrollGridView>(R.id.gvDetailTypes)
                if (t?.detailType_list == null || t.detailType_list.isEmpty()) {
                    gridView?.visibility = View.GONE
                    return
                }
                gridView?.adapter = object : com.minhua.jijiehao2.base.adapter.abslistview.BaseCommonAdapter<CircleServiceDetailTypeBean>(this@MoreTypeActivity, R.layout.item_more_type_detail, t.detailType_list) {
                    override fun convert(holder: ViewHolder?, t: CircleServiceDetailTypeBean?) {
                        holder?.setText(R.id.tvType, t?.item_value)
                        holder?.setOnClickListener(R.id.tvType, { BusinessListActivity.intentStart(this@MoreTypeActivity, t?.item_code!!) })
                    }
                }
            }
        }
        rvContent.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContent.adapter = adapter
    }

    override fun initData() {
        val data = intent.getSerializableExtra("data") as ArrayList<CircleServiceTypeBean>
        if (data.isNotEmpty()) {
            listData.addAll(data)
            adapter?.notifyDataSetChanged()
        }
    }
}
